<template>
  <div class="login_bg">
    <a-card class="login_card">
      <div class="login_box_title">
        <div class="text">账号登录</div>
        <div class="english">User account login</div>
      </div>
      <a-form :model="loginForm" ref="formRef" :rules="rulesForm">
        <a-form-item name="account">
          <a-input v-model:value="loginForm.account" prefix-icon="el-icon-user" placeholder="账号"/>
        </a-form-item>
        <a-form-item name="password">
          <a-input-password show-password v-model:value="loginForm.password" prefix-icon="el-icon-lock"
                            placeholder="密码"/>
        </a-form-item>
        <a-form-item name="validateCode">
          <div style="display: flex">
            <a-input v-model:value="loginForm.validateCode" style="width: 70%" placeholder="验证码"/>
            <img @click="fetchCode" :src="codeUrl" style="width: 30%;height: 100%;cursor: pointer"/>
          </div>
        </a-form-item>
        <a-form-item>
          <a-button :loading="loginLoading" style="width: 100%" @click="submitLoginForm()" type="primary">
            登录
          </a-button>
        </a-form-item>
      </a-form>
    </a-card>

  </div>
</template>

<script>
import {ref, reactive, onMounted} from 'vue'
import router from "@/router";
import {message} from "ant-design-vue";
import {getLoginCode, onLogin} from "../../api";

export default {
  name: "Login",
  setup() {
    const codeUrl = ref('')
    const loginForm = reactive({
      key: '',
      account: '',
      password: '',
      validateCode: ''
    })
    const loginLoading = ref(false)
    const formRef = ref(null)
    const rulesForm = reactive({
      account: {required: true, message: '请输入账号', trigger: 'blur'},
      password: {required: true, message: '请输入密码', trigger: 'blur'},
      validateCode: {required: true, message: '请输入验证码', trigger: 'blur'},
    })
    const fetchCode = async () => {
      loginForm.key = new Date().getTime().toString() + GetRandomNum(0, 9999)
      const res = await getLoginCode({key: loginForm.key})
      if (res) {
        codeUrl.value = 'data:image/png;base64,' + btoa(new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''))
      }
    }
    const GetRandomNum = (Min, Max) => {
      let Range = Max - Min;
      let Rand = Math.random();
      return (Min + Math.round(Rand * Range)).toString();
    }
    const submitLoginForm = async () => {
      formRef.value.validate().then(async () => {
        loginLoading.value = true
        const res = await onLogin(loginForm)
        if (res.code === 200) {
          message.success('登录成功')
          const {systemUser, token, permission} = res.data
          sessionStorage.setItem('userInfo', JSON.stringify(systemUser))
          sessionStorage.setItem('token', token)
          sessionStorage.setItem('menuList', JSON.stringify(permission))
          loginLoading.value = false
          router.push('/index')
        } else {
          loginLoading.value = false
          fetchCode()
        }
      })


    }
    onMounted(() => {
      fetchCode()
      document.onkeydown = (ev) => {
        if (ev.key === 'Enter') {
          submitLoginForm()
        }
      }
    })
    return {
      loginForm,
      codeUrl,
      submitLoginForm,
      rulesForm,
      fetchCode,
      loginLoading,
      formRef
    }
  }
}
</script>

<style scoped lang="less">
@media screen and (max-width: 1024px ) {
  .login_bg {
    min-height: 100vh;
  }

  .login_card {
    left: 50% !important;
    width: 320px !important;
    transform: translate(-50%, -50%) !important;
  }
}

.login_bg {
  min-height: 100vh;
  min-width: 100%;
  //background-image: url("../../assets/login_bg.png");
  background-size: 100% 100%;
  background-color: #001529;

  .login_card {
    position: absolute;
    right: 10%;
    top: 50%;
    transform: translateY(-50%);
    width: 350px;

    .login_box_title {
      text-align: center;
      margin-bottom: 20px;

      .text {
        font-size: 24px;
        letter-spacing: 1px;
        color: #66bfff;
      }

      .english {
        color: #66bfff;
        margin-top: 5px;
        font-size: 10px;
      }
    }

    .login_box_title::before {
      content: '';
      width: 50px;
      height: 1px;
      border-bottom: 1px solid;
      display: block;
      position: absolute;
      left: 35px;
      top: 45px;
      color: #66bfff;
    }

    .login_box_title::after {
      content: '';
      width: 50px;
      height: 1px;
      border-bottom: 1px solid;
      display: block;
      position: absolute;
      right: 35px;
      top: 45px;
      color: #66bfff;
    }

  }
}
</style>
